import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import os


def download_boston_data():
    """下载波士顿房价数据集并保存到本地"""
    import pandas as pd
    import os

    # 检查数据文件是否已存在
    if not os.path.exists('boston_housing.csv'):
        print("正在下载波士顿房价数据集...")

        try:
            url = 'http://lib.stat.cmu.edu/datasets/boston'

            # 读取原始数据（所有行）
            raw_data = pd.read_csv(url, sep='\s+', skiprows=21, header=None)

            # 重新组织数据格式 - 原始数据中每两行对应一个完整的样本
            data = []
            for i in range(0, len(raw_data), 2):
                # 获取两行的数据并合并
                row_part1 = raw_data.iloc[i].dropna().values
                row_part2 = raw_data.iloc[i + 1].dropna().values
                combined_row = np.concatenate([row_part1, row_part2])
                data.append(combined_row)

            # 定义列名
            column_names = [
                'CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS',
                'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT', 'PRICE'  # 注意这里用PRICE而不是MEDV
            ]

            # 创建DataFrame
            df = pd.DataFrame(data, columns=column_names)

            # 保存到本地
            df.to_csv('boston_housing.csv', index=False)
            print("数据已保存到 boston_housing.csv")

        except Exception as e:
            print(f"下载数据失败: {e}")
            print("尝试从sklearn加载数据...")
            try:
                from sklearn.datasets import load_boston
                boston = load_boston()
                X = boston.data
                y = boston.target

                feature_names = boston.feature_names
                df = pd.DataFrame(X, columns=feature_names)
                df['PRICE'] = y
                df.to_csv('boston_housing.csv', index=False)
                print("数据已从sklearn加载并保存到 boston_housing.csv")
            except:
                print("sklearn加载也失败，请检查网络或安装")
                return None
    else:
        print("使用本地已存在的数据文件 boston_housing.csv")

    # 读取数据
    df = pd.read_csv('boston_housing.csv')
    return df

df = download_boston_data()
print(df)